package com.sy.pet.service;

import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sy.pet.config.excetion.ServiceExcetion;
import com.sy.pet.dto.UserDTO;
import com.sy.pet.dto.UserQueryDTO;
import com.sy.pet.entity.User;
import com.sy.pet.mapper.UserMapper;
import com.sy.pet.vo.UserPageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liangSY
 * @since 2024-09-04
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    public User findByAccount(String account, String channel) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getAccount, account);
        queryWrapper.eq(User::getChannel, channel);
        User user = this.baseMapper.selectOne(queryWrapper);
        return user;
    }

    public IPage<UserPageVo> pageOfUser(UserQueryDTO dto) {
        Page<UserPageVo> page = new Page(dto.getPage(), dto.getSize());
        IPage<UserPageVo> userPageVos = baseMapper.pageOfUser(page, dto);
        return userPageVos;
    }

    public void register(UserDTO dto) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getAccount, dto.getAccount());
        User user = getOne(queryWrapper);
        if (!Objects.isNull(user)) {
            throw new ServiceExcetion(1000, "账号已存在");
        }
        baseMapper.insert(User.builder().
                account(dto.getAccount()).
                password(BCrypt.hashpw(dto.getPassword(), BCrypt.gensalt()))
                .nickname(dto.getNickName())
                .email(dto.getEmail())
                .mobile(dto.getMobile())
                .createTime(LocalDateTime.now())
                .channel(dto.getChannel())
                .build());
    }

    public void remove(String id) {
        User user = baseMapper.selectById(id);
        if (Objects.isNull(user)) {
            return;
        }
        user.setActive(false);
        baseMapper.updateById(user);
    }

    public void editUser(String id, UserDTO dto) {
        User user = getById(id);
        if (Objects.isNull(user)) {
            throw new NullPointerException("请确认需要修改的用户");
        }
        user.setNickname(dto.getNickName());
        user.setMobile(dto.getMobile());
        user.setEmail(dto.getEmail());
        updateById(user);
    }

    public static void main(String[] args) {
        String hashpw = BCrypt.hashpw("123456", BCrypt.gensalt());
        System.out.println(hashpw);

    }

}
